
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>03-Express · 千锋大前端小册-GP19-Node.js</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="古艺散人">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="04-Koa2.html" />
    
    
    <link rel="prev" href="02-yarn.html" />
    

    
        <link rel="shortcut icon" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    01-Node.js基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02-yarn.html">
            
                <a href="02-yarn.html">
            
                    
                    02-Yarn入门
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3" data-path="03-Express.html">
            
                <a href="03-Express.html">
            
                    
                    03-Express
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04-Koa2.html">
            
                <a href="04-Koa2.html">
            
                    
                    04-Koa2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05-MongoDB.html">
            
                <a href="05-MongoDB.html">
            
                    
                    05-MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06-socket.html">
            
                <a href="06-socket.html">
            
                    
                    06-socket编程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    高级
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    01-Node中间件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../advanced/02-Node事件循环.html">
            
                <a href="../advanced/02-Node事件循环.html">
            
                    
                    02-Node事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../advanced/03-Node中间层.html">
            
                <a href="../advanced/03-Node中间层.html">
            
                    
                    03-Node中间层
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../thanks/">
            
                <a href="../thanks/">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >03-Express</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#express"><b></b>Express</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;&#x7279;&#x8272;"><b></b>&#x4E00;&#x3001;&#x7279;&#x8272;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;&#x5B89;&#x88C5;"><b></b>&#x4E8C;&#x3001;&#x5B89;&#x88C5;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;"><b></b>&#x4E09;&#x3001;Hello world &#x5B9E;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><b></b>&#x56DB;&#x3001;&#x8DEF;&#x7531;</a></li><li><span class="title-icon "></span><a href="#&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;"><b></b>&#x4E94;&#x3001;&#x5229;&#x7528; Express &#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;"><b></b>&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;"><b></b>3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;"><b></b>4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><b></b>&#x4E03;&#x3001;&#x5728; Express &#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;</a></li></ul></ul></ul></div><a href="#express" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="express"><a name="express" class="anchor-navigation-ex-anchor" href="#express"><i class="fa fa-link" aria-hidden="true"></i></a><a name="express" class="plugin-anchor" href="#express"><i class="fa fa-link" aria-hidden="true"></i></a>Express</h1>
<p>&#x57FA;&#x4E8E; Node.js &#x5E73;&#x53F0;&#xFF0C;&#x5FEB;&#x901F;&#x3001;&#x5F00;&#x653E;&#x3001;&#x6781;&#x7B80;&#x7684; web &#x5F00;&#x53D1;&#x6846;&#x67B6;&#x3002;</p>
<pre><code>$ npm install express --save
</code></pre><h3 id="&#x4E00;&#x3001;&#x7279;&#x8272;"><a name="&#x4E00;&#x3001;&#x7279;&#x8272;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;&#x7279;&#x8272;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;&#x7279;&#x8272;" class="plugin-anchor" href="#&#x4E00;&#x3001;&#x7279;&#x8272;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;&#x7279;&#x8272;</h3>
<h4 id="1&#x3001;web-&#x5E94;&#x7528;"><a name="1&#x3001;web-&#x5E94;&#x7528;" class="anchor-navigation-ex-anchor" href="#1&#x3001;web-&#x5E94;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;web-&#x5E94;&#x7528;" class="plugin-anchor" href="#1&#x3001;web-&#x5E94;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;Web &#x5E94;&#x7528;</h4>
<p>Express &#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E; Node.js &#x5E73;&#x53F0;&#x7684;&#x6781;&#x7B80;&#x3001;&#x7075;&#x6D3B;&#x7684; web &#x5E94;&#x7528;&#x5F00;&#x53D1;&#x6846;&#x67B6;&#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x4E00;&#x7CFB;&#x5217;&#x5F3A;&#x5927;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x5E2E;&#x52A9;&#x4F60;&#x521B;&#x5EFA;&#x5404;&#x79CD; Web &#x548C;&#x79FB;&#x52A8;&#x8BBE;&#x5907;&#x5E94;&#x7528;&#x3002;</p>
<h4 id="2&#x3001;api"><a name="2&#x3001;api" class="anchor-navigation-ex-anchor" href="#2&#x3001;api"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;api" class="plugin-anchor" href="#2&#x3001;api"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;API</h4>
<p>&#x4E30;&#x5BCC;&#x7684; HTTP &#x5FEB;&#x6377;&#x65B9;&#x6CD5;&#x548C;&#x4EFB;&#x610F;&#x6392;&#x5217;&#x7EC4;&#x5408;&#x7684; Connect &#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x8BA9;&#x4F60;&#x521B;&#x5EFA;&#x5065;&#x58EE;&#x3001;&#x53CB;&#x597D;&#x7684; API &#x53D8;&#x5F97;&#x65E2;&#x5FEB;&#x901F;&#x53C8;&#x7B80;&#x5355;&#x3002;</p>
<h4 id="3&#x3001;&#x6027;&#x80FD;"><a name="3&#x3001;&#x6027;&#x80FD;" class="anchor-navigation-ex-anchor" href="#3&#x3001;&#x6027;&#x80FD;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;&#x6027;&#x80FD;" class="plugin-anchor" href="#3&#x3001;&#x6027;&#x80FD;"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;&#x6027;&#x80FD;</h4>
<p>Express &#x4E0D;&#x5BF9; Node.js &#x5DF2;&#x6709;&#x7684;&#x7279;&#x6027;&#x8FDB;&#x884C;&#x4E8C;&#x6B21;&#x62BD;&#x8C61;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x5728;&#x5B83;&#x4E4B;&#x4E0A;&#x6269;&#x5C55;&#x4E86; Web &#x5E94;&#x7528;&#x6240;&#x9700;&#x7684;&#x57FA;&#x672C;&#x529F;&#x80FD;&#x3002;</p>
<h3 id="&#x4E8C;&#x3001;&#x5B89;&#x88C5;"><a name="&#x4E8C;&#x3001;&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;&#x5B89;&#x88C5;" class="plugin-anchor" href="#&#x4E8C;&#x3001;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;&#x5B89;&#x88C5;</h3>
<p>&#x9996;&#x5148;&#x5047;&#x5B9A;&#x4F60;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x4E86; Node.js&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x4E3A;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF0C;&#x7136;&#x540E;&#x8FDB;&#x5165;&#x6B64;&#x76EE;&#x5F55;&#x5E76;&#x5C06;&#x5176;&#x4F5C;&#x4E3A;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;&#x3002;</p>
<pre><code>$ mkdir myapp
$ cd myapp
</code></pre><p>&#x901A;&#x8FC7; npm init &#x547D;&#x4EE4;&#x4E3A;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; package.json &#x6587;&#x4EF6;&#x3002; &#x6B32;&#x4E86;&#x89E3; package.json &#x662F;&#x5982;&#x4F55;&#x8D77;&#x4F5C;&#x7528;&#x7684;&#xFF0C;&#x8BF7;&#x53C2;&#x8003; Specifics of npm&#x2019;s package.json handling&#x3002;</p>
<pre><code>$ npm init
</code></pre><p>&#x6B64;&#x547D;&#x4EE4;&#x5C06;&#x8981;&#x6C42;&#x4F60;&#x8F93;&#x5165;&#x51E0;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4F8B;&#x5982;&#x6B64;&#x5E94;&#x7528;&#x7684;&#x540D;&#x79F0;&#x548C;&#x7248;&#x672C;&#x3002; &#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6309;&#x201C;&#x56DE;&#x8F66;&#x201D;&#x952E;&#x63A5;&#x53D7;&#x9ED8;&#x8BA4;&#x8BBE;&#x7F6E;&#x5373;&#x53EF;&#xFF0C;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x9664;&#x5916;&#xFF1A;</p>
<pre><code>entry point: (index.js)
</code></pre><p>&#x952E;&#x5165; app.js &#x6216;&#x8005;&#x4F60;&#x6240;&#x5E0C;&#x671B;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x8FD9;&#x662F;&#x5F53;&#x524D;&#x5E94;&#x7528;&#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x91C7;&#x7528;&#x9ED8;&#x8BA4;&#x7684; index.js &#x6587;&#x4EF6;&#x540D;&#xFF0C;&#x53EA;&#x9700;&#x6309;&#x201C;&#x56DE;&#x8F66;&#x201D;&#x952E;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x5B89;&#x88C5; Express &#x5E76;&#x5C06;&#x5176;&#x4FDD;&#x5B58;&#x5230;&#x4F9D;&#x8D56;&#x5217;&#x8868;&#x4E2D;&#xFF1A;</p>
<pre><code>$ npm install express --save
</code></pre><p>&#x5982;&#x679C;&#x53EA;&#x662F;&#x4E34;&#x65F6;&#x5B89;&#x88C5; Express&#xFF0C;&#x4E0D;&#x60F3;&#x5C06;&#x5B83;&#x6DFB;&#x52A0;&#x5230;&#x4F9D;&#x8D56;&#x5217;&#x8868;&#x4E2D;&#xFF0C;&#x53EA;&#x9700;&#x7565;&#x53BB; --save &#x53C2;&#x6570;&#x5373;&#x53EF;&#xFF1A;</p>
<pre><code>$ npm install express
</code></pre><blockquote>
<p>&#x5B89;&#x88C5; Node &#x6A21;&#x5757;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6307;&#x5B9A;&#x4E86; --save &#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x6A21;&#x5757;&#x5C06;&#x88AB;&#x6DFB;&#x52A0;&#x5230; package.json &#x6587;&#x4EF6;&#x4E2D; dependencies &#x4F9D;&#x8D56;&#x5217;&#x8868;&#x4E2D;&#x3002; &#x7136;&#x540E;&#x901A;&#x8FC7; npm install &#x547D;&#x4EE4;&#x5373;&#x53EF;&#x81EA;&#x52A8;&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x5217;&#x8868;&#x4E2D;&#x6240;&#x5217;&#x51FA;&#x7684;&#x6240;&#x6709;&#x6A21;&#x5757;&#x3002;</p>
</blockquote>
<h3 id="&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;"><a name="&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;" class="plugin-anchor" href="#&#x4E09;&#x3001;hello-world-&#x5B9E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;Hello world &#x5B9E;&#x4F8B;</h3>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x8D77;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x57FA;&#x672C;&#x7684; Express &#x5E94;&#x7528;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x8FD9;&#x91CC;&#x6240;&#x521B;&#x5EFA;&#x662F;&#x4E00;&#x4E2A;&#x6700;&#x6700;&#x7B80;&#x5355;&#x7684; Express &#x5E94;&#x7528;&#xFF0C;&#x5E76;&#x4E14;&#x4EC5;&#x4EC5;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6587;&#x4EF6; &#x2014; &#x548C;&#x901A;&#x8FC7; Express &#x5E94;&#x7528;&#x751F;&#x6210;&#x5668; &#x6240;&#x521B;&#x5EFA;&#x7684;&#x5E94;&#x7528;<em>&#x5B8C;&#x5168;&#x4E0D;&#x4E00;&#x6837;</em>&#xFF0C;Express &#x5E94;&#x7528;&#x751F;&#x6210;&#x5668;&#x6240;&#x521B;&#x5EFA;&#x7684;&#x5E94;&#x7528;&#x6846;&#x67B6;&#x5305;&#x542B;&#x591A; JavaScript &#x6587;&#x4EF6;&#x3001;Jade &#x6A21;&#x677F;&#x548C;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x7528;&#x9014;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#x3002;</p>
<p>&#x8FDB;&#x5165; myapp &#x76EE;&#x5F55;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; app.js &#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x4E0B;&#x5217;&#x4EE3;&#x7801;&#x590D;&#x5236;&#x8FDB;&#x53BB;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> app = express();

app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello World!&apos;</span>);
});

<span class="hljs-keyword">var</span> server = app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> host = server.address().address;
  <span class="hljs-keyword">var</span> port = server.address().port;

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Example app listening at http://%s:%s&apos;</span>, host, port);
});
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x5E76;&#x76D1;&#x542C;&#x4ECE; 3000 &#x7AEF;&#x53E3;&#x8FDB;&#x5165;&#x7684;&#x6240;&#x6709;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#x3002;&#x4ED6;&#x5C06;&#x5BF9;&#x6240;&#x6709; (/) URL &#x6216; &#x8DEF;&#x7531; &#x8FD4;&#x56DE; &#x201C;Hello World!&#x201D; &#x5B57;&#x7B26;&#x4E32;&#x3002;&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x6240;&#x6709;&#x8DEF;&#x5F84;&#x5168;&#x90E8;&#x8FD4;&#x56DE; 404 Not Found&#x3002;</p>
<blockquote>
<p>req (&#x8BF7;&#x6C42;) &#x548C; res (&#x54CD;&#x5E94;) &#x4E0E; Node &#x63D0;&#x4F9B;&#x7684;&#x5BF9;&#x8C61;&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8C03;&#x7528; req.pipe()&#x3001;req.on(&apos;data&apos;, callback) &#x4EE5;&#x53CA;&#x4EFB;&#x4F55; Node &#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<p>&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x547D;&#x4EE4;&#x542F;&#x52A8;&#x6B64;&#x5E94;&#x7528;&#xFF1A;</p>
<pre><code>$ node app.js
</code></pre><p>&#x7136;&#x540E;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x6253;&#x5F00; <a href="http://localhost:3000/" target="_blank">http://localhost:3000/</a> &#x5E76;&#x67E5;&#x770B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x3002;</p>
<h3 id="&#x56DB;&#x3001;&#x8DEF;&#x7531;"><a name="&#x56DB;&#x3001;&#x8DEF;&#x7531;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;&#x3001;&#x8DEF;&#x7531;" class="plugin-anchor" href="#&#x56DB;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x8DEF;&#x7531;</h3>
<p>&#x8DEF;&#x7531;&#x662F;&#x6307;&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x5E94;&#x7528;&#x7684;&#x7AEF;&#x70B9;&#xFF08;URIs&#xFF09;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x54CD;&#x5E94;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x8DEF;&#x7531;&#x662F;&#x7531;&#x4E00;&#x4E2A; URI&#x3001;HTTP &#x8BF7;&#x6C42;&#xFF08;GET&#x3001;POST&#x7B49;&#xFF09;&#x548C;&#x82E5;&#x5E72;&#x4E2A;&#x53E5;&#x67C4;&#x7EC4;&#x6210;&#xFF0C;&#x5B83;&#x7684;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A; app.METHOD(path, [callback...], callback)&#xFF0C; app &#x662F; express &#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C; METHOD &#x662F;&#x4E00;&#x4E2A; HTTP &#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#xFF0C; path &#x662F;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;&#x8DEF;&#x5F84;&#xFF0C; callback &#x662F;&#x5F53;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x65F6;&#x8981;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x672C;&#x7684;&#x8DEF;&#x7531;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> app = express();

<span class="hljs-comment">// respond with &quot;hello world&quot; when a GET request is made to the homepage</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;hello world&apos;</span>);
});
</code></pre>
<h4 id="1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;"><a name="1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;" class="plugin-anchor" href="#1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;</h4>
<p>&#x8DEF;&#x7531;&#x65B9;&#x6CD5;&#x6E90;&#x4E8E; HTTP &#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#xFF0C;&#x548C; express &#x5B9E;&#x4F8B;&#x76F8;&#x5173;&#x8054;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x4E3A;&#x5E94;&#x7528;&#x8DDF;&#x8DEF;&#x5F84;&#x5B9A;&#x4E49;&#x7684; GET &#x548C; POST &#x8BF7;&#x6C42;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// GET method route</span>
<span class="hljs-comment">// &#x5BF9;&#x7F51;&#x7AD9;&#x9996;&#x9875;&#x7684;&#x8BBF;&#x95EE;&#x8FD4;&#x56DE; &quot;Hello World!&quot; &#x5B57;&#x6837;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello World!&apos;</span>)
})

<span class="hljs-comment">// &#x7F51;&#x7AD9;&#x9996;&#x9875;&#x63A5;&#x53D7; POST &#x8BF7;&#x6C42;</span>
app.post(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Got a POST request&apos;</span>)
})

<span class="hljs-comment">// /user &#x8282;&#x70B9;&#x63A5;&#x53D7; PUT &#x8BF7;&#x6C42;</span>
app.put(<span class="hljs-string">&apos;/user&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Got a PUT request at /user&apos;</span>)
})

<span class="hljs-comment">// /user &#x8282;&#x70B9;&#x63A5;&#x53D7; DELETE &#x8BF7;&#x6C42;</span>
app.delete(<span class="hljs-string">&apos;/user&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Got a DELETE request at /user&apos;</span>)
})
</code></pre>
<p>Express &#x5B9A;&#x4E49;&#x4E86;&#x5982;&#x4E0B;&#x548C; HTTP &#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x7684;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;&#xFF1A; get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, &#x548C; connect&#x3002;</p>
<blockquote>
<p>&#x6709;&#x4E9B;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;&#x540D;&#x4E0D;&#x662F;&#x5408;&#x89C4;&#x7684; JavaScript &#x53D8;&#x91CF;&#x540D;&#xFF0C;&#x6B64;&#x65F6;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x8BB0;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A; app[&apos;m-search&apos;](&apos;/&apos;, function ...</p>
</blockquote>
<p>app.all() &#x662F;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x7684;&#x8DEF;&#x7531;&#x65B9;&#x6CD5;&#xFF0C;&#x6CA1;&#x6709;&#x4EFB;&#x4F55; HTTP &#x65B9;&#x6CD5;&#x4E0E;&#x5176;&#x5BF9;&#x5E94;&#xFF0C;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;&#x8DEF;&#x5F84;&#x4E0A;&#x7684;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x52A0;&#x8F7D;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6765;&#x81EA; &#x201C;/secret&#x201D; &#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x4E0D;&#x7BA1;&#x4F7F;&#x7528; GET&#x3001;POST&#x3001;PUT&#x3001;DELETE &#x6216;&#x5176;&#x4ED6;&#x4EFB;&#x4F55; http &#x6A21;&#x5757;&#x652F;&#x6301;&#x7684; HTTP &#x8BF7;&#x6C42;&#xFF0C;&#x53E5;&#x67C4;&#x90FD;&#x4F1A;&#x5F97;&#x5230;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-js">app.all(<span class="hljs-string">&apos;/secret&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Accessing the secret section ...&apos;</span>)
  next(); <span class="hljs-comment">// pass control to the next handler</span>
})
</code></pre>
<h4 id="2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;"><a name="2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;" class="plugin-anchor" href="#2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;</h4>
<p>&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x548C;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x4E00;&#x8D77;&#x5B9A;&#x4E49;&#x4E86;&#x8BF7;&#x6C42;&#x7684;&#x7AEF;&#x70B9;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x6A21;&#x5F0F;&#x6216;&#x8005;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>Express &#x4F7F;&#x7528; path-to-regexp &#x5339;&#x914D;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x6587;&#x6863;&#x67E5;&#x9605;&#x6240;&#x6709;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x7684;&#x65B9;&#x6CD5;&#x3002; Express Route Tester &#x662F;&#x6D4B;&#x8BD5;&#x57FA;&#x672C; Express &#x8DEF;&#x5F84;&#x7684;&#x597D;&#x5DE5;&#x5177;&#xFF0C;&#x4F46;&#x4E0D;&#x652F;&#x6301;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x3002;</p>
<blockquote>
<p>&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x662F;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x3002;</p>
</blockquote>
<p>&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5339;&#x914D;&#x6839;&#x8DEF;&#x5F84;&#x7684;&#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;root&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; /about &#x8DEF;&#x5F84;&#x7684;&#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/about&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;about&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; /random.text &#x8DEF;&#x5F84;&#x7684;&#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/random.text&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;random.text&apos;</span>);
});
</code></pre>
<p>&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x6A21;&#x5F0F;&#x7684;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5339;&#x914D; acd &#x548C; abcd</span>
app.get(<span class="hljs-string">&apos;/ab?cd&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;ab?cd&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; abcd&#x3001;abbcd&#x3001;abbbcd&#x7B49;</span>
app.get(<span class="hljs-string">&apos;/ab+cd&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;ab+cd&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; abcd&#x3001;abxcd&#x3001;abRABDOMcd&#x3001;ab123cd&#x7B49;</span>
app.get(<span class="hljs-string">&apos;/ab*cd&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;ab*cd&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; /abe &#x548C; /abcde</span>
app.get(<span class="hljs-string">&apos;/ab(cd)?e&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
 res.send(<span class="hljs-string">&apos;ab(cd)?e&apos;</span>);
});
</code></pre>
<blockquote>
<p>&#x5B57;&#x7B26; ?&#x3001;+&#x3001;* &#x548C; () &#x662F;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5B50;&#x96C6;&#xFF0C;- &#x548C; . &#x5728;&#x57FA;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8DEF;&#x5F84;&#x4E2D;&#x6309;&#x7167;&#x5B57;&#x9762;&#x503C;&#x89E3;&#x91CA;&#x3002;</p>
</blockquote>
<p>&#x4F7F;&#x7528;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5339;&#x914D;&#x4EFB;&#x4F55;&#x8DEF;&#x5F84;&#x4E2D;&#x542B;&#x6709; a &#x7684;&#x8DEF;&#x5F84;&#xFF1A;</span>
app.get(<span class="hljs-regexp">/a/</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;/a/&apos;</span>);
});

<span class="hljs-comment">// &#x5339;&#x914D; butterfly&#x3001;dragonfly&#xFF0C;&#x4E0D;&#x5339;&#x914D; butterflyman&#x3001;dragonfly man&#x7B49;</span>
app.get(<span class="hljs-regexp">/.*fly$/</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;/.*fly$/&apos;</span>);
});
</code></pre>
<h4 id="3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;"><a name="3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;" class="anchor-navigation-ex-anchor" href="#3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;" class="plugin-anchor" href="#3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;&#x8DEF;&#x7531;&#x53E5;&#x67C4;</h4>
<p>&#x53EF;&#x4EE5;&#x4E3A;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x63D0;&#x4F9B;&#x591A;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5176;&#x884C;&#x4E3A;&#x7C7B;&#x4F3C; &#x4E2D;&#x95F4;&#x4EF6;&#x3002;&#x552F;&#x4E00;&#x7684;&#x533A;&#x522B;&#x662F;&#x8FD9;&#x4E9B;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6709;&#x53EF;&#x80FD;&#x8C03;&#x7528; next(&apos;route&apos;) &#x65B9;&#x6CD5;&#x800C;&#x7565;&#x8FC7;&#x5176;&#x4ED6;&#x8DEF;&#x7531;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x8BE5;&#x673A;&#x5236;&#x4E3A;&#x8DEF;&#x7531;&#x5B9A;&#x4E49;&#x524D;&#x63D0;&#x6761;&#x4EF6;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x73B0;&#x6709;&#x8DEF;&#x5F84;&#x4E0A;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x6CA1;&#x6709;&#x610F;&#x4E49;&#xFF0C;&#x5219;&#x53EF;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x5269;&#x4E0B;&#x7684;&#x8DEF;&#x5F84;&#x3002;</p>
<p>&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#x6709;&#x591A;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3001;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6570;&#x7EC4;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x4E24;&#x8005;&#x6DF7;&#x5408;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;.</p>
<p>&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF1A;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/example/a&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello from A!&apos;</span>);
});
</code></pre>
<p>&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF08;&#x8BB0;&#x5F97;&#x6307;&#x5B9A; next &#x5BF9;&#x8C61;&#xFF09;&#xFF1A;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/example/b&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;response will be sent by the next function ...&apos;</span>);
  next();
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello from B!&apos;</span>);
});
</code></pre>
<p>&#x4F7F;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6570;&#x7EC4;&#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> cb0 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;CB0&apos;</span>)
  next()
}

<span class="hljs-keyword">var</span> cb1 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;CB1&apos;</span>)
  next()
}

<span class="hljs-keyword">var</span> cb2 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello from C!&apos;</span>)
}

app.get(<span class="hljs-string">&apos;/example/c&apos;</span>, [cb0, cb1, cb2])
</code></pre>
<p>&#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x548C;&#x51FD;&#x6570;&#x6570;&#x7EC4;&#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> cb0 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;CB0&apos;</span>)
  next()
}

<span class="hljs-keyword">var</span> cb1 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;CB1&apos;</span>)
  next()
}

app.get(<span class="hljs-string">&apos;/example/d&apos;</span>, [cb0, cb1], <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;response will be sent by the next function ...&apos;</span>)
  next()
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Hello from D!&apos;</span>)
})
</code></pre>
<h4 id="4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;"><a name="4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;" class="plugin-anchor" href="#4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x3001;&#x54CD;&#x5E94;&#x65B9;&#x6CD5;</h4>
<p>&#x4E0B;&#x8868;&#x4E2D;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#xFF08;res&#xFF09;&#x7684;&#x65B9;&#x6CD5;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x8FD4;&#x56DE;&#x54CD;&#x5E94;&#xFF0C;&#x7EC8;&#x7ED3;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x7684;&#x5FAA;&#x73AF;&#x3002;&#x5982;&#x679C;&#x5728;&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#x4E2D;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x4E5F;&#x4E0D;&#x8C03;&#x7528;&#xFF0C;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x4F1A;&#x4E00;&#x76F4;&#x6302;&#x8D77;&#x3002;</p>
<table>
<thead>
<tr>
<th>&#x65B9;&#x6CD5;</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td>res.download()</td>
<td>&#x63D0;&#x793A;&#x4E0B;&#x8F7D;&#x6587;&#x4EF6;&#x3002;</td>
</tr>
<tr>
<td>res.end()</td>
<td>&#x7EC8;&#x7ED3;&#x54CD;&#x5E94;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x3002;</td>
</tr>
<tr>
<td>res.json()</td>
<td>&#x53D1;&#x9001;&#x4E00;&#x4E2A; JSON &#x683C;&#x5F0F;&#x7684;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>res.jsonp()</td>
<td>&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x652F;&#x6301; JSONP &#x7684; JSON &#x683C;&#x5F0F;&#x7684;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>res.redirect()</td>
<td>&#x91CD;&#x5B9A;&#x5411;&#x8BF7;&#x6C42;&#x3002;</td>
</tr>
<tr>
<td>res.render()</td>
<td>&#x6E32;&#x67D3;&#x89C6;&#x56FE;&#x6A21;&#x677F;&#x3002;</td>
</tr>
<tr>
<td>res.send()</td>
<td>&#x53D1;&#x9001;&#x5404;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>res.sendFile</td>
<td>&#x4EE5;&#x516B;&#x4F4D;&#x5B57;&#x8282;&#x6D41;&#x7684;&#x5F62;&#x5F0F;&#x53D1;&#x9001;&#x6587;&#x4EF6;&#x3002;</td>
</tr>
<tr>
<td>res.sendStatus()</td>
<td>&#x8BBE;&#x7F6E;&#x54CD;&#x5E94;&#x72B6;&#x6001;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x5F62;&#x5F0F;&#x4F5C;&#x4E3A;&#x54CD;&#x5E94;&#x4F53;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x53D1;&#x9001;&#x3002;</td>
</tr>
</tbody>
</table>
<h4 id="5&#x3001;approute"><a name="5&#x3001;approute" class="anchor-navigation-ex-anchor" href="#5&#x3001;approute"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x3001;approute" class="plugin-anchor" href="#5&#x3001;approute"><i class="fa fa-link" aria-hidden="true"></i></a>5&#x3001;app.route()</h4>
<p>&#x53EF;&#x4F7F;&#x7528; app.route() &#x521B;&#x5EFA;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x7684;&#x94FE;&#x5F0F;&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#x3002;&#x7531;&#x4E8E;&#x8DEF;&#x5F84;&#x5728;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x6307;&#x5B9A;&#xFF0C;&#x8FD9;&#x6837;&#x505A;&#x6709;&#x52A9;&#x4E8E;&#x521B;&#x5EFA;&#x6A21;&#x5757;&#x5316;&#x7684;&#x8DEF;&#x7531;&#xFF0C;&#x800C;&#x4E14;&#x51CF;&#x5C11;&#x4E86;&#x4EE3;&#x7801;&#x5197;&#x4F59;&#x548C;&#x62FC;&#x5199;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528; app.route() &#x5B9A;&#x4E49;&#x4E86;&#x94FE;&#x5F0F;&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#x3002;</p>
<pre><code class="lang-js">app.route(<span class="hljs-string">&apos;/book&apos;</span>)
  .get(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
    res.send(<span class="hljs-string">&apos;Get a random book&apos;</span>);
  })
  .post(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
    res.send(<span class="hljs-string">&apos;Add a book&apos;</span>);
  })
  .put(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
    res.send(<span class="hljs-string">&apos;Update the book&apos;</span>);
  });
</code></pre>
<h4 id="6&#x3001;expressrouter"><a name="6&#x3001;expressrouter" class="anchor-navigation-ex-anchor" href="#6&#x3001;expressrouter"><i class="fa fa-link" aria-hidden="true"></i></a><a name="6&#x3001;expressrouter" class="plugin-anchor" href="#6&#x3001;expressrouter"><i class="fa fa-link" aria-hidden="true"></i></a>6&#x3001;express.Router</h4>
<p>&#x53EF;&#x4F7F;&#x7528; express.Router &#x7C7B;&#x521B;&#x5EFA;&#x6A21;&#x5757;&#x5316;&#x3001;&#x53EF;&#x6302;&#x8F7D;&#x7684;&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#x3002;Router &#x5B9E;&#x4F8B;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x548C;&#x8DEF;&#x7531;&#x7CFB;&#x7EDF;&#xFF0C;&#x56E0;&#x6B64;&#x5E38;&#x79F0;&#x5176;&#x4E3A;&#x4E00;&#x4E2A; &#x201C;mini-app&#x201D;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x5B9E;&#x4F8B;&#x7A0B;&#x5E8F;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x6A21;&#x5757;&#xFF0C;&#x5E76;&#x52A0;&#x8F7D;&#x4E86;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E9B;&#x8DEF;&#x7531;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x5B83;&#x4EEC;&#x6302;&#x8F7D;&#x81F3;&#x5E94;&#x7528;&#x7684;&#x8DEF;&#x5F84;&#x4E0A;&#x3002;</p>
<p>&#x5728; app &#x76EE;&#x5F55;&#x4E0B;&#x521B;&#x5EFA;&#x540D;&#x4E3A; birds.js &#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> router = express.Router();

<span class="hljs-comment">// &#x8BE5;&#x8DEF;&#x7531;&#x4F7F;&#x7528;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;</span>
router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">timeLog</span>(<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Time: &apos;</span>, <span class="hljs-built_in">Date</span>.now());
  next();
});
<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x7F51;&#x7AD9;&#x4E3B;&#x9875;&#x7684;&#x8DEF;&#x7531;</span>
router.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;Birds home page&apos;</span>);
});
<span class="hljs-comment">// &#x5B9A;&#x4E49; about &#x9875;&#x9762;&#x7684;&#x8DEF;&#x7531;</span>
router.get(<span class="hljs-string">&apos;/about&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">&apos;About birds&apos;</span>);
});

<span class="hljs-built_in">module</span>.exports = router;
</code></pre>
<p>&#x7136;&#x540E;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x52A0;&#x8F7D;&#x8DEF;&#x7531;&#x6A21;&#x5757;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> birds = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./birds&apos;</span>)
...
app.use(<span class="hljs-string">&apos;/birds&apos;</span>, birds)
</code></pre>
<p>&#x5E94;&#x7528;&#x5373;&#x53EF;&#x5904;&#x7406;&#x53D1;&#x81EA; /birds &#x548C; /birds/about &#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x4E14;&#x8C03;&#x7528;&#x4E3A;&#x8BE5;&#x8DEF;&#x7531;&#x6307;&#x5B9A;&#x7684; timeLog &#x4E2D;&#x95F4;&#x4EF6;&#x3002;</p>
<h3 id="&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;"><a name="&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;" class="plugin-anchor" href="#&#x4E94;&#x3001;&#x5229;&#x7528;-express-&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94;&#x3001;&#x5229;&#x7528; Express &#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;</h3>
<p>&#x901A;&#x8FC7; Express &#x5185;&#x7F6E;&#x7684; express.static &#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5730;&#x6258;&#x7BA1;&#x9759;&#x6001;&#x6587;&#x4EF6;&#xFF0C;&#x4F8B;&#x5982;&#x56FE;&#x7247;&#x3001;CSS&#x3001;JavaScript &#x6587;&#x4EF6;&#x7B49;&#x3002;</p>
<p>&#x5C06;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x6240;&#x5728;&#x7684;&#x76EE;&#x5F55;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9; express.static &#x4E2D;&#x95F4;&#x4EF6;&#x5C31;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x7684;&#x8BBF;&#x95EE;&#x4E86;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE;&#x5728; public &#x76EE;&#x5F55;&#x653E;&#x7F6E;&#x4E86;&#x56FE;&#x7247;&#x3001;CSS &#x548C; JavaScript &#x6587;&#x4EF6;&#xFF0C;&#x4F60;&#x5C31;&#x53EF;&#x4EE5;&#xFF1A;</p>
<pre><code class="lang-js">app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>))
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;public &#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x7684;&#x6587;&#x4EF6;&#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x4E86;&#x3002;</p>
<pre><code class="lang-js">http:<span class="hljs-comment">//localhost:3000/images/kitten.jpg</span>
http:<span class="hljs-comment">//localhost:3000/css/style.css</span>
http:<span class="hljs-comment">//localhost:3000/js/app.js</span>
http:<span class="hljs-comment">//localhost:3000/images/bg.png</span>
http:<span class="hljs-comment">//localhost:3000/hello.html</span>
</code></pre>
<blockquote>
<p>&#x6240;&#x6709;&#x6587;&#x4EF6;&#x7684;&#x8DEF;&#x5F84;&#x90FD;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x5B58;&#x653E;&#x76EE;&#x5F55;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x5B58;&#x653E;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#x540D;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x5728; URL &#x4E2D;&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x4F60;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x5B58;&#x653E;&#x5728;&#x591A;&#x4E2A;&#x76EE;&#x5F55;&#x4E0B;&#x9762;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x591A;&#x6B21;&#x8C03;&#x7528; express.static &#x4E2D;&#x95F4;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-js">app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>))
app.use(express.static(<span class="hljs-string">&apos;files&apos;</span>))
</code></pre>
<p>&#x8BBF;&#x95EE;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x65F6;&#xFF0C;express.static &#x4E2D;&#x95F4;&#x4EF6;&#x4F1A;&#x6839;&#x636E;&#x76EE;&#x5F55;&#x6DFB;&#x52A0;&#x7684;&#x987A;&#x5E8F;&#x67E5;&#x627E;&#x6240;&#x9700;&#x7684;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x6240;&#x6709;&#x901A;&#x8FC7; express.static &#x8BBF;&#x95EE;&#x7684;&#x6587;&#x4EF6;&#x90FD;&#x5B58;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x201C;&#x865A;&#x62DF;&#xFF08;virtual&#xFF09;&#x201D;&#x76EE;&#x5F55;&#xFF08;&#x5373;&#x76EE;&#x5F55;&#x6839;&#x672C;&#x4E0D;&#x5B58;&#x5728;&#xFF09;&#x4E0B;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E3A;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x76EE;&#x5F55;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x6302;&#x8F7D;&#x8DEF;&#x5F84;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code>app.use(&apos;/static&apos;, express.static(&apos;public&apos;))
</code></pre><p>&#x73B0;&#x5728;&#xFF0C;&#x4F60;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5E26;&#x6709; &#x201C;/static&#x201D; &#x524D;&#x7F00;&#x7684;&#x5730;&#x5740;&#x6765;&#x8BBF;&#x95EE; public &#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x7684;&#x6587;&#x4EF6;&#x4E86;&#x3002;</p>
<pre><code>http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
</code></pre><h3 id="&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;"><a name="&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516D;&#x3001;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;</h3>
<p>Express &#x662F;&#x4E00;&#x4E2A;&#x81EA;&#x8EAB;&#x529F;&#x80FD;&#x6781;&#x7B80;&#xFF0C;&#x5B8C;&#x5168;&#x662F;&#x7531;&#x8DEF;&#x7531;&#x548C;&#x4E2D;&#x95F4;&#x4EF6;&#x6784;&#x6210;&#x4E00;&#x4E2A;&#x7684; web &#x5F00;&#x53D1;&#x6846;&#x67B6;&#xFF1A;&#x4ECE;&#x672C;&#x8D28;&#x4E0A;&#x6765;&#x8BF4;&#xFF0C;&#x4E00;&#x4E2A; Express &#x5E94;&#x7528;&#x5C31;&#x662F;&#x5728;&#x8C03;&#x7528;&#x5404;&#x79CD;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;</p>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#xFF08;Middleware&#xFF09; &#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#xFF08;request object (req)&#xFF09;, &#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#xFF08;response object (res)&#xFF09;, &#x548C; web &#x5E94;&#x7528;&#x4E2D;&#x5904;&#x4E8E;&#x8BF7;&#x6C42;-&#x54CD;&#x5E94;&#x5FAA;&#x73AF;&#x6D41;&#x7A0B;&#x4E2D;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x4E00;&#x822C;&#x88AB;&#x547D;&#x540D;&#x4E3A; next &#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x529F;&#x80FD;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li>&#x6267;&#x884C;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x3002;</li>
<li>&#x4FEE;&#x6539;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x7EC8;&#x7ED3;&#x8BF7;&#x6C42;-&#x54CD;&#x5E94;&#x5FAA;&#x73AF;&#x3002;</li>
<li>&#x8C03;&#x7528;&#x5806;&#x6808;&#x4E2D;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;</li>
</ul>
<p>&#x5982;&#x679C;&#x5F53;&#x524D;&#x4E2D;&#x95F4;&#x4EF6;&#x6CA1;&#x6709;&#x7EC8;&#x7ED3;&#x8BF7;&#x6C42;-&#x54CD;&#x5E94;&#x5FAA;&#x73AF;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x8C03;&#x7528; next() &#x65B9;&#x6CD5;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5426;&#x5219;&#x8BF7;&#x6C42;&#x5C31;&#x4F1A;&#x6302;&#x8D77;&#x3002;</p>
<p>Express &#x5E94;&#x7528;&#x53EF;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x51E0;&#x79CD;&#x4E2D;&#x95F4;&#x4EF6;&#xFF1A;</p>
<ul>
<li>&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;</li>
<li>&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;</li>
<li>&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;</li>
<li>&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;</li>
<li>&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x5219;&#x6302;&#x8F7D;&#x8DEF;&#x5F84;&#xFF0C;&#x53EF;&#x5728;&#x5E94;&#x7528;&#x7EA7;&#x522B;&#x6216;&#x8DEF;&#x7531;&#x7EA7;&#x522B;&#x88C5;&#x8F7D;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x88C5;&#x5728;&#x4E00;&#x7CFB;&#x5217;&#x4E2D;&#x95F4;&#x4EF6;&#x51FD;&#x6570;&#xFF0C;&#x4ECE;&#x800C;&#x5728;&#x4E00;&#x4E2A;&#x6302;&#x8F7D;&#x70B9;&#x4E0A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5B50;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#x3002;</p>
<h4 id="1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><a name="1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;</h4>
<p>&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;&#x7ED1;&#x5B9A;&#x5230; app &#x5BF9;&#x8C61; &#x4F7F;&#x7528; app.use() &#x548C; app.METHOD()&#xFF0C; &#x5176;&#x4E2D;&#xFF0C; METHOD &#x662F;&#x9700;&#x8981;&#x5904;&#x7406;&#x7684; HTTP &#x8BF7;&#x6C42;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4F8B;&#x5982; GET, PUT, POST &#x7B49;&#x7B49;&#xFF0C;&#x5168;&#x90E8;&#x5C0F;&#x5199;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> app = express()

<span class="hljs-comment">// &#x6CA1;&#x6709;&#x6302;&#x8F7D;&#x8DEF;&#x5F84;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5E94;&#x7528;&#x7684;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x8BE5;&#x4E2D;&#x95F4;&#x4EF6;</span>
app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Time:&apos;</span>, <span class="hljs-built_in">Date</span>.now())
  next()
})

<span class="hljs-comment">// &#x6302;&#x8F7D;&#x81F3; /user/:id &#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x4EFB;&#x4F55;&#x6307;&#x5411; /user/:id &#x7684;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x5B83;</span>
app.use(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request Type:&apos;</span>, req.method)
  next()
})

<span class="hljs-comment">// &#x8DEF;&#x7531;&#x548C;&#x53E5;&#x67C4;&#x51FD;&#x6570;(&#x4E2D;&#x95F4;&#x4EF6;&#x7CFB;&#x7EDF;)&#xFF0C;&#x5904;&#x7406;&#x6307;&#x5411; /user/:id &#x7684; GET &#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  res.send(<span class="hljs-string">&apos;USER&apos;</span>)
})
</code></pre>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x5728;&#x4E00;&#x4E2A;&#x6302;&#x8F7D;&#x70B9;&#x88C5;&#x8F7D;&#x4E00;&#x7EC4;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#xFF0C;&#x5BF9;&#x4EFB;&#x4F55;&#x6307;&#x5411; /user/:id &#x7684; HTTP &#x8BF7;&#x6C42;&#x6253;&#x5370;&#x51FA;&#x76F8;&#x5173;&#x4FE1;&#x606F;</span>
app.use(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request URL:&apos;</span>, req.originalUrl)
  next()
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request Type:&apos;</span>, req.method)
  next()
})
</code></pre>
<p>&#x4F5C;&#x4E3A;&#x4E2D;&#x95F4;&#x4EF6;&#x7CFB;&#x7EDF;&#x7684;&#x8DEF;&#x7531;&#x53E5;&#x67C4;&#xFF0C;&#x4F7F;&#x5F97;&#x4E3A;&#x8DEF;&#x5F84;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x8DEF;&#x7531;&#x6210;&#x4E3A;&#x53EF;&#x80FD;&#x3002;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x4E3A;&#x6307;&#x5411; /user/:id &#x7684; GET &#x8BF7;&#x6C42;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x8DEF;&#x7531;&#x3002;&#x7B2C;&#x4E8C;&#x4E2A;&#x8DEF;&#x7531;&#x867D;&#x7136;&#x4E0D;&#x4F1A;&#x5E26;&#x6765;&#x4EFB;&#x4F55;&#x95EE;&#x9898;&#xFF0C;&#x4F46;&#x5374;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x56E0;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5DF2;&#x7ECF;&#x7EC8;&#x6B62;&#x4E86;&#x8BF7;&#x6C42;-&#x54CD;&#x5E94;&#x5FAA;&#x73AF;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#xFF0C;&#x5904;&#x7406;&#x6307;&#x5411; /user/:id &#x7684; GET &#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;ID:&apos;</span>, req.params.id)
  next()
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  res.send(<span class="hljs-string">&apos;User Info&apos;</span>)
})

<span class="hljs-comment">// &#x5904;&#x7406; /user/:id&#xFF0C; &#x6253;&#x5370;&#x51FA;&#x7528;&#x6237; id</span>
app.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  res.end(req.params.id)
})
</code></pre>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x5728;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#x4E2D;&#x8DF3;&#x8FC7;&#x5269;&#x4F59;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x8C03;&#x7528; next(&apos;route&apos;) &#x65B9;&#x6CD5;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x3002; &#x6CE8;&#x610F;&#xFF1A; next(&apos;route&apos;) &#x53EA;&#x5BF9;&#x4F7F;&#x7528; app.VERB() &#x6216; router.VERB() &#x52A0;&#x8F7D;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x6709;&#x6548;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#xFF0C;&#x5904;&#x7406;&#x6307;&#x5411; /user/:id &#x7684; GET &#x8BF7;&#x6C42;</span>
app.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-comment">// &#x5982;&#x679C; user id &#x4E3A; 0, &#x8DF3;&#x5230;&#x4E0B;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;</span>
  <span class="hljs-keyword">if</span> (req.params.id == <span class="hljs-number">0</span>) next(<span class="hljs-string">&apos;route&apos;</span>)
  <span class="hljs-comment">// &#x5426;&#x5219;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x6808;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</span>
  <span class="hljs-keyword">else</span> next() <span class="hljs-comment">//</span>
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-comment">// &#x6E32;&#x67D3;&#x5E38;&#x89C4;&#x9875;&#x9762;</span>
  res.render(<span class="hljs-string">&apos;regular&apos;</span>)
});

<span class="hljs-comment">// &#x5904;&#x7406; /user/:id&#xFF0C; &#x6E32;&#x67D3;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x9875;&#x9762;</span>
app.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  res.render(<span class="hljs-string">&apos;special&apos;</span>)
})
</code></pre>
<h4 id="2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><a name="2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;</h4>
<p>&#x8DEF;&#x7531;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;&#x548C;&#x5E94;&#x7528;&#x7EA7;&#x4E2D;&#x95F4;&#x4EF6;&#x4E00;&#x6837;&#xFF0C;&#x53EA;&#x662F;&#x5B83;&#x7ED1;&#x5B9A;&#x7684;&#x5BF9;&#x8C61;&#x4E3A; express.Router()&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> router = express.Router()
</code></pre>
<p>&#x8DEF;&#x7531;&#x7EA7;&#x4F7F;&#x7528; router.use() &#x6216; router.VERB() &#x52A0;&#x8F7D;&#x3002;</p>
<p>&#x4E0A;&#x8FF0;&#x5728;&#x5E94;&#x7528;&#x7EA7;&#x521B;&#x5EFA;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x7CFB;&#x7EDF;&#xFF0C;&#x53EF;&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#x6539;&#x5199;&#x4E3A;&#x8DEF;&#x7531;&#x7EA7;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> app = express()
<span class="hljs-keyword">var</span> router = express.Router()

<span class="hljs-comment">// &#x6CA1;&#x6709;&#x6302;&#x8F7D;&#x8DEF;&#x5F84;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x901A;&#x8FC7;&#x8BE5;&#x8DEF;&#x7531;&#x7684;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x8BE5;&#x4E2D;&#x95F4;&#x4EF6;</span>
router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Time:&apos;</span>, <span class="hljs-built_in">Date</span>.now())
  next()
})

<span class="hljs-comment">// &#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#xFF0C;&#x663E;&#x793A;&#x4EFB;&#x4F55;&#x6307;&#x5411; /user/:id &#x7684; HTTP &#x8BF7;&#x6C42;&#x7684;&#x4FE1;&#x606F;</span>
router.use(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request URL:&apos;</span>, req.originalUrl)
  next()
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request Type:&apos;</span>, req.method)
  next()
})

<span class="hljs-comment">// &#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x6808;&#xFF0C;&#x5904;&#x7406;&#x6307;&#x5411; /user/:id &#x7684; GET &#x8BF7;&#x6C42;</span>
router.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-comment">// &#x5982;&#x679C; user id &#x4E3A; 0, &#x8DF3;&#x5230;&#x4E0B;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;</span>
  <span class="hljs-keyword">if</span> (req.params.id == <span class="hljs-number">0</span>) next(<span class="hljs-string">&apos;route&apos;</span>)
  <span class="hljs-comment">// &#x8D1F;&#x8D23;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x6808;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</span>
  <span class="hljs-keyword">else</span> next() <span class="hljs-comment">//</span>
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-comment">// &#x6E32;&#x67D3;&#x5E38;&#x89C4;&#x9875;&#x9762;</span>
  res.render(<span class="hljs-string">&apos;regular&apos;</span>)
})

<span class="hljs-comment">// &#x5904;&#x7406; /user/:id&#xFF0C; &#x6E32;&#x67D3;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x9875;&#x9762;</span>
router.get(<span class="hljs-string">&apos;/user/:id&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.log(req.params.id)
  res.render(<span class="hljs-string">&apos;special&apos;</span>)
})

<span class="hljs-comment">// &#x5C06;&#x8DEF;&#x7531;&#x6302;&#x8F7D;&#x81F3;&#x5E94;&#x7528;</span>
app.use(<span class="hljs-string">&apos;/&apos;</span>, router)
</code></pre>
<h3 id="3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;"><a name="3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;</h3>
<blockquote>
<p>&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;&#x6709; 4 &#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5B9A;&#x4E49;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;&#x65F6;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x8FD9; 4 &#x4E2A;&#x53C2;&#x6570;&#x3002;&#x5373;&#x4F7F;&#x4E0D;&#x9700;&#x8981; next &#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x5FC5;&#x987B;&#x5728;&#x7B7E;&#x540D;&#x4E2D;&#x58F0;&#x660E;&#x5B83;&#xFF0C;&#x5426;&#x5219;&#x4E2D;&#x95F4;&#x4EF6;&#x4F1A;&#x88AB;&#x8BC6;&#x522B;&#x4E3A;&#x4E00;&#x4E2A;&#x5E38;&#x89C4;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x4E0D;&#x80FD;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3002;</p>
</blockquote>
<p>&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;&#x548C;&#x5176;&#x4ED6;&#x4E2D;&#x95F4;&#x4EF6;&#x5B9A;&#x4E49;&#x7C7B;&#x4F3C;&#xFF0C;&#x53EA;&#x662F;&#x8981;&#x4F7F;&#x7528; 4 &#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x800C;&#x4E0D;&#x662F; 3 &#x4E2A;&#xFF0C;&#x5176;&#x7B7E;&#x540D;&#x5982;&#x4E0B;&#xFF1A; (err, req, res, next)&#x3002;</p>
<pre><code class="lang-js">app.use(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, req, res, next</span>) </span>{
  <span class="hljs-built_in">console</span>.error(err.stack)
  res.status(<span class="hljs-number">500</span>).send(<span class="hljs-string">&apos;Something broke!&apos;</span>)
})
</code></pre>
<h3 id="4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;"><a name="4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x3001;&#x5185;&#x7F6E;&#x4E2D;&#x95F4;&#x4EF6;</h3>
<p>&#x4ECE; 4.x &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;, Express &#x5DF2;&#x7ECF;&#x4E0D;&#x518D;&#x4F9D;&#x8D56; Connect &#x4E86;&#x3002;&#x9664;&#x4E86; express.static, Express &#x4EE5;&#x524D;&#x5185;&#x7F6E;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x73B0;&#x5728;&#x5DF2;&#x7ECF;&#x5168;&#x90E8;&#x5355;&#x72EC;&#x4F5C;&#x4E3A;&#x6A21;&#x5757;&#x5B89;&#x88C5;&#x4F7F;&#x7528;&#x4E86;&#x3002;&#x8BF7;&#x53C2;&#x8003; &#x4E2D;&#x95F4;&#x4EF6;&#x5217;&#x8868;&#x3002;</p>
<p><strong>express.static(root, [options])</strong></p>
<p>express.static &#x662F; Express &#x552F;&#x4E00;&#x5185;&#x7F6E;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;&#x5B83;&#x57FA;&#x4E8E; serve-static&#xFF0C;&#x8D1F;&#x8D23;&#x5728; Express &#x5E94;&#x7528;&#x4E2D;&#x63D0;&#x6258;&#x7BA1;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x53C2;&#x6570; root &#x6307;&#x63D0;&#x4F9B;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x7684;&#x6839;&#x76EE;&#x5F55;&#x3002;</p>
<p>&#x53EF;&#x9009;&#x7684; options &#x53C2;&#x6570;&#x62E5;&#x6709;&#x5982;&#x4E0B;&#x5C5E;&#x6027;&#x3002;</p>
<table>
<thead>
<tr>
<th>&#x5C5E;&#x6027;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x7F3A;&#x7701;&#x503C;</th>
</tr>
</thead>
<tbody>
<tr>
<td>dotfiles</td>
<td>&#x662F;&#x5426;&#x5BF9;&#x5916;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x540D;&#x4EE5;&#x70B9;&#xFF08;.&#xFF09;&#x5F00;&#x5934;&#x7684;&#x6587;&#x4EF6;&#x3002;&#x53EF;&#x9009;&#x503C;&#x4E3A; &#x201C;allow&#x201D;&#x3001;&#x201C;deny&#x201D; &#x548C; &#x201C;ignore&#x201D;</td>
<td>String</td>
<td>&#x201C;ignore&#x201D;</td>
</tr>
<tr>
<td>etag</td>
<td>&#x662F;&#x5426;&#x542F;&#x7528; etag &#x751F;&#x6210;</td>
<td>Boolean</td>
<td>true</td>
</tr>
<tr>
<td>extensions</td>
<td>&#x8BBE;&#x7F6E;&#x6587;&#x4EF6;&#x6269;&#x5C55;&#x540D;&#x5907;&#x4EFD;&#x9009;&#x9879;</td>
<td>Array</td>
<td>[]</td>
</tr>
<tr>
<td>index</td>
<td>&#x53D1;&#x9001;&#x76EE;&#x5F55;&#x7D22;&#x5F15;&#x6587;&#x4EF6;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A; false &#x7981;&#x7528;&#x76EE;&#x5F55;&#x7D22;&#x5F15;&#x3002;</td>
<td>Mixed</td>
<td>&#x201C;index.html&#x201D;</td>
</tr>
<tr>
<td>lastModified</td>
<td>&#x8BBE;&#x7F6E; Last-Modified &#x5934;&#x4E3A;&#x6587;&#x4EF6;&#x5728;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E0A;&#x7684;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65E5;&#x671F;&#x3002;&#x53EF;&#x80FD;&#x503C;&#x4E3A; true &#x6216; false&#x3002;</td>
<td>Boolean</td>
<td>true</td>
</tr>
<tr>
<td>maxAge</td>
<td>&#x4EE5;&#x6BEB;&#x79D2;&#x6216;&#x8005;&#x5176;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x8BBE;&#x7F6E; Cache-Control &#x5934;&#x7684; max-age &#x5C5E;&#x6027;&#x3002;</td>
<td>Number</td>
<td>0</td>
</tr>
<tr>
<td>redirect</td>
<td>&#x5F53;&#x8DEF;&#x5F84;&#x4E3A;&#x76EE;&#x5F55;&#x65F6;&#xFF0C;&#x91CD;&#x5B9A;&#x5411;&#x81F3; &#x201C;/&#x201D;&#x3002;</td>
<td>Boolean</td>
<td>true</td>
</tr>
<tr>
<td>setHeaders</td>
<td>&#x8BBE;&#x7F6E; HTTP &#x5934;&#x4EE5;&#x63D0;&#x4F9B;&#x6587;&#x4EF6;&#x7684;&#x51FD;&#x6570;&#x3002;</td>
<td>Function     </td>
</tr>
</tbody>
</table>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4F7F;&#x7528;&#x4E86; express.static &#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5176;&#x4E2D;&#x7684; options &#x5BF9;&#x8C61;&#x7ECF;&#x8FC7;&#x4E86;&#x7CBE;&#x5FC3;&#x7684;&#x8BBE;&#x8BA1;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> options = {
  dotfiles: <span class="hljs-string">&apos;ignore&apos;</span>,
  etag: <span class="hljs-literal">false</span>,
  extensions: [<span class="hljs-string">&apos;htm&apos;</span>, <span class="hljs-string">&apos;html&apos;</span>],
  index: <span class="hljs-literal">false</span>,
  maxAge: <span class="hljs-string">&apos;1d&apos;</span>,
  redirect: <span class="hljs-literal">false</span>,
  setHeaders: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">res, path, stat</span>) </span>{
    res.set(<span class="hljs-string">&apos;x-timestamp&apos;</span>, <span class="hljs-built_in">Date</span>.now())
  }
}

app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>, options))
</code></pre>
<p>&#x6BCF;&#x4E2A;&#x5E94;&#x7528;&#x53EF;&#x6709;&#x591A;&#x4E2A;&#x9759;&#x6001;&#x76EE;&#x5F55;&#x3002;</p>
<pre><code class="lang-js">app.use(express.static(<span class="hljs-string">&apos;public&apos;</span>))
app.use(express.static(<span class="hljs-string">&apos;uploads&apos;</span>))
app.use(express.static(<span class="hljs-string">&apos;files&apos;</span>))
</code></pre>
<h4 id="5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;"><a name="5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>5&#x3001;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;</h4>
<p>&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x7B2C;&#x4E09;&#x65B9;&#x4E2D;&#x95F4;&#x4EF6;&#x4ECE;&#x800C;&#x4E3A; Express &#x5E94;&#x7528;&#x589E;&#x52A0;&#x66F4;&#x591A;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x5B89;&#x88C5;&#x6240;&#x9700;&#x529F;&#x80FD;&#x7684; node &#x6A21;&#x5757;&#xFF0C;&#x5E76;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x52A0;&#x8F7D;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5E94;&#x7528;&#x7EA7;&#x52A0;&#x8F7D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8DEF;&#x7531;&#x7EA7;&#x52A0;&#x8F7D;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5B89;&#x88C5;&#x5E76;&#x52A0;&#x8F7D;&#x4E86;&#x4E00;&#x4E2A;&#x89E3;&#x6790; cookie &#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#xFF1A; cookie-parser</p>
<pre><code>$ npm install cookie-parser
</code></pre><pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>)
<span class="hljs-keyword">var</span> app = express()
<span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>)

<span class="hljs-comment">// &#x52A0;&#x8F7D;&#x7528;&#x4E8E;&#x89E3;&#x6790; cookie &#x7684;&#x4E2D;&#x95F4;&#x4EF6;</span>
app.use(cookieParser())
</code></pre>
<h3 id="&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><a name="&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="anchor-navigation-ex-anchor" href="#&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="plugin-anchor" href="#&#x4E03;&#x3001;&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E03;&#x3001;&#x5728; Express &#x4E2D;&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;</h3>
<p>&#x9700;&#x8981;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x8FDB;&#x884C;&#x5982;&#x4E0B;&#x8BBE;&#x7F6E;&#x624D;&#x80FD;&#x8BA9; Express &#x6E32;&#x67D3;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#xFF1A;</p>
<ul>
<li>views, &#x653E;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A; app.set(&apos;views&apos;, &apos;./views&apos;)</li>
<li>view engine, &#x6A21;&#x677F;&#x5F15;&#x64CE;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A; app.set(&apos;view engine&apos;, &apos;ejs&apos;)</li>
</ul>
<h4 id="art-template"><a name="art-template" class="anchor-navigation-ex-anchor" href="#art-template"><i class="fa fa-link" aria-hidden="true"></i></a><a name="art-template" class="plugin-anchor" href="#art-template"><i class="fa fa-link" aria-hidden="true"></i></a>art-template</h4>
<p>art-template for express 4.x.</p>
<h5 id="1&#x3001;install"><a name="1&#x3001;install" class="anchor-navigation-ex-anchor" href="#1&#x3001;install"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;install" class="plugin-anchor" href="#1&#x3001;install"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;Install</h5>
<pre><code>npm install --save art-template
npm install --save express-art-template
</code></pre><h5 id="2&#x3001;example"><a name="2&#x3001;example" class="anchor-navigation-ex-anchor" href="#2&#x3001;example"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;example" class="plugin-anchor" href="#2&#x3001;example"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;Example</h5>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>)
<span class="hljs-keyword">var</span> app = express()

<span class="hljs-comment">// view engine setup</span>
app.engine(<span class="hljs-string">&apos;art&apos;</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express-art-template&apos;</span>))
app.set(<span class="hljs-string">&apos;view&apos;</span>, {
    debug: process.env.NODE_ENV !== <span class="hljs-string">&apos;production&apos;</span>
})
app.set(<span class="hljs-string">&apos;views&apos;</span>, path.join(__dirname, <span class="hljs-string">&apos;views&apos;</span>))
app.set(<span class="hljs-string">&apos;view engine&apos;</span>, <span class="hljs-string">&apos;art&apos;</span>)

<span class="hljs-comment">// routes</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.render(<span class="hljs-string">&apos;index.art&apos;</span>, {
        user: {
            name: <span class="hljs-string">&apos;aui&apos;</span>,
            tags: [<span class="hljs-string">&apos;art&apos;</span>, <span class="hljs-string">&apos;template&apos;</span>, <span class="hljs-string">&apos;nodejs&apos;</span>]
        }
    })
})
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="02-yarn.html" class="navigation navigation-prev " aria-label="Previous page: 02-Yarn入门">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="04-Koa2.html" class="navigation navigation-next " aria-label="Next page: 04-Koa2">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"03-Express","level":"1.2.3","depth":2,"next":{"title":"04-Koa2","level":"1.2.4","depth":2,"path":"basics/04-Koa2.md","ref":"./basics/04-Koa2.md","articles":[]},"previous":{"title":"02-Yarn入门","level":"1.2.2","depth":2,"path":"basics/02-yarn.md","ref":"./basics/02-yarn.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","sharing-plus","anchor-navigation-ex","favicon","insert-logo","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/lurongtao/felixbooks-gp19-Node.js"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"insert-logo":{"url":"/felixbooks-gp19-node.js/images/node.js.jpg","style":"background: skyblue; width: 100%; height: 100%;"}},"theme":"default","author":"古艺散人","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"千锋大前端小册-GP19-Node.js","language":"zh-hans","links":{},"gitbook":"3.2.3","description":"Node.js复习"},"file":{"path":"basics/03-Express.md","mtime":"2020-06-25T00:17:54.660Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-15T09:28:56.165Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

